package com.ruoyi.app.controller.user;

import com.ruoyi.app.business.account.CompanyAccountBusiness;
import com.ruoyi.app.business.project.ProjectBusiness;
import com.ruoyi.app.business.user.UserBusiness;
import com.ruoyi.app.config.annotation.OperationAuth;
import com.ruoyi.app.domain.account.CompanyAccount;
import com.ruoyi.app.domain.my.DemanderMy;
import com.ruoyi.app.domain.project.ProjectSimple;
import com.ruoyi.app.domain.register.CompanyRegister;
import com.ruoyi.app.domain.user.add.UserInfoAdd;
import com.ruoyi.app.domain.user.add.UserInfoView;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.web.domain.APIResult;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.service.account.query.CompanyAccountQuery;
import com.ruoyi.service.project.query.ProjectQueryForDemander;
import com.ruoyi.service.user.enums.UserTypeEnum;
import com.ruoyi.service.user.query.UserQuery;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目方用户控制器
 *
 * @author Zane
 * @since 2025-09-27
 */
@OperationAuth(value = {UserTypeEnum.DEMANDER_USER})
@RestController
@RequestMapping("/demander")
public class DemanderController {

  @Resource
  private UserBusiness userBusiness;
  @Resource
  private ProjectBusiness projectBusiness;
  @Autowired
  private CompanyAccountBusiness companyAccountBusiness;


  /**
   * 绑定公司
   *
   */
  @OperationAuth(value = {UserTypeEnum.DEMANDER_USER}, demanderHasCompanyCheck = false)
  @GetMapping("bindCompany")
  public APIResult<Void> bindCompany(CompanyRegister companyRegister) {
    userBusiness.bindCompany(companyRegister);
    return APIResult.success();
  }

  /**
   * 账户管理-账户列表
   */
  @PostMapping("userManageList")
  public TableDataInfo<UserInfoView> demanderUserManageList(
      @Validated @RequestBody UserQuery query) {
    final CommonPageInfo<UserInfoView> userView = userBusiness.demanderUserManageList(query);
    return CommonUtils.buildTableData(userView);
  }

  /**
   * 创建项目方
   */
  @PostMapping("addUser")
  public APIResult<Void> addUser(@RequestBody UserInfoAdd userInfo) {
    userBusiness.addUserByDemander(userInfo);
    return APIResult.success();
  }

  /**
   * 我的页面
   */
  @OperationAuth(value = {UserTypeEnum.DEMANDER_USER}, demanderCompanyPassedCheck = false)
  @PostMapping("myInfo")
  public APIResult<DemanderMy> demanderMyInfo() {
    DemanderMy demanderMy = userBusiness.demanderMyInfo();
    return APIResult.success(demanderMy);
  }

  /**
   * 已合作项目列表
   */
  @PostMapping("/project/listForCooperation")
  public TableDataInfo<ProjectSimple> listForCooperation(
      @Validated @RequestBody ProjectQueryForDemander projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.demanderListForCooperation(
        projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 待验收项目列表
   */
  @PostMapping("/project/listForVery")
  public TableDataInfo<ProjectSimple> listForVery(
      @Validated @RequestBody ProjectQueryForDemander projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.demanderListForVery(projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 执行中项目列表
   */
  @PostMapping("/project/forExecution")
  public TableDataInfo<ProjectSimple> forExecution(
      @Validated @RequestBody ProjectQueryForDemander projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.demanderListForExecution(projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 异常记录项目列表
   */
  @PostMapping("/project/forSudden")
  public TableDataInfo<ProjectSimple> forSudden(
      @Validated @RequestBody ProjectQueryForDemander projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.demanderListForSudden(projectQuery);
    return CommonUtils.buildTableData(projects);
  }


  /**
   * 收支明细
   */
  @PostMapping("/company/accountList")
  public TableDataInfo<CompanyAccount> companyAccountList(
      @Validated @RequestBody CompanyAccountQuery query) {
    CommonPageInfo<CompanyAccount> accountList = companyAccountBusiness.companyAccountList(query);
    return CommonUtils.buildTableData(accountList);
  }
}
